<?php 
	session_start();
	$var1= $_SESSION["registrado"];
	$var2= $_SESSION["administrador"];
	if ($var1 == null): {
		echo ("User not registered");
	} elseif ($var2 == 0): { 
			echo ("User without permissions");
	} elseif (empty($_POST['name']) && empty($_POST['surname'])&& empty($_POST['email'])):{
	    echo ("You can only access this page from new member and name and surname can´t be null");
	}else : {
?>


<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet"	media="screen">

<title>My project manager</title>

<style type="text/css">
  body {
    padding-top: 60px;
    padding-bottom: 40px;
  }
  .sidebar-nav {
    padding: 9px 0;
  
  }
    .container {
    padding-top: 100px;
  }

    .finish {
        padding-top: 200px;
        padding-left: 25px;
        padding-right: 25px;
  }
  

  
  @media (max-width: 980px) {
    /* Enable use of floated navbar text */
    .navbar-text.pull-right {
      float: none;
      padding-left: 5px;
      padding-right: 5px;
    }
  }
</style>

</head>

<body>
<?php include ('configuration.php'); ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>
    
<div class="span12">
<div class="navbar navbar-inverse navbar-fixed-top">
	<div class="navbar-inner">
		<div class="container-fluid">
			<button type="button" class="btn btn-navbar" data-toggle="collapse"
				data-target=".nav-collapse">
				<span class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>
			<a class="brand"href="http://localhost/bases--grupo10/homeAdmin.php?">My project manager</a>
			<div class="nav-collapse collapse">
				<p class="navbar-text pull-right">
				
					<a href="http://localhost/bases--grupo10/logout.php?" class="navbar-link">Log out</a>
				</p>
				<ul class="nav">
					<li class="active"><a href="http://localhost/bases--grupo10/homeAdmin.php?">Home</a></li>
					<li><a href="http://localhost/bases--grupo10/about.php?">About</a></li>
					<li><a href="http://localhost/bases--grupo10/contact.php?">Contact</a></li>
				</ul>
			</div>
		</div>
	</div>
</div>
</div>

<?php
//funcion que hara el boton de enviar, guardar los datos del formulario en la base de datos y redirigir
//a la pagina principal

// Primero comprobamos que ningún campo esté vacío y que todos los campos existan.
 // Si entramos es que todo se ha realizado correctamente
  $name=$_POST['name'];
  $surnames=$_POST['surname'];

   if(empty($name) || empty($surnames)) {
       ?>
       <script type="text/javascript">
          alert("Error: name and surnames are required");
          history.back();
        </script>
       <?php 
   }
   else { 
//compruebo que el nombre y apellidos no tengan mas longitud de la adecuada
       if(strlen($name)>15) {
       ?><script type="text/javascript">
              alert("Name is too long. Change name");    
              history.back();
         </script> 
       <?php
       }
       if(strlen($surnames)>35) {
           ?><script type="text/javascript">
                 alert("Surnames is too long. Change surnames");    
                 history.back();
            </script>
        <?php
        }
        
        else {
   //si todos los campos son correctos, agregamos al usuario a la base de datos de miembros
            if (!empty($_POST['email'])){
                if(strlen($_POST['email'])>50) {
                ?><script type="text/javascript">
                         alert("Email is too long");    
                         history.back();
                    </script>
                    <?php
                 }
                 else{
                    mysql_query("INSERT INTO project_member (name_member,surnames_member, mail_member)
                    VALUES ('{$_POST['name']}','{$_POST['surname']}', '{$_POST['email']}')");
                }    
            }
            else{
                mysql_query("INSERT INTO project_member (name_member,surnames_member)
                    VALUES ('{$_POST['name']}','{$_POST['surname']}')") or die('Error, query failed');      
            }

            ?>   
            <div class="span12">
                <h1 class="muted"  >New member</h1>
                <h1 >Member created </h1>	
            </div>	
            <div class="finish">  
    	        <a class="btn btn-primary" type="button" href="http://localhost/bases--grupo10/newMember.php?" >Create more</a>
    		    <a class="btn btn-primary" type="button" href="http://localhost/bases--grupo10/homeAdmin.php?" >Finish</a>
            </div>	
    		<?php 

        }     
    }

}

endif;?>